package cn.tx.redis7.test;

import cn.tx.redis7.config.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.resps.GeoRadiusResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author jizenan
 * @date 2025/6/3
 * Description:
 */
public class GeoDemo {

    public static void main(String[] args) throws Exception {
        Jedis jedis = RedisUtils.getJedis();
        // GEO 数据结构使用的 key
        String geoKey = "cities:location";
        // 1. 添加地理位置信息 (GEOADD)
        // 添加单个位置
        jedis.geoadd(geoKey, 116.405285, 39.904989, "Beijing");
        // 批量添加位置
        Map<String, GeoCoordinate> locations = new HashMap<>();
        locations.put("Shanghai", new GeoCoordinate(121.472644, 31.231706));
        locations.put("Guangzhou", new GeoCoordinate(113.280637, 23.125178));
        locations.put("Shenzhen", new GeoCoordinate(114.057868, 22.543096));
        locations.put("Chengdu", new GeoCoordinate(104.065735, 30.659462));
        jedis.geoadd(geoKey, locations);

        System.out.println("已添加城市位置信息");
        // 2. 获取地理位置信息 (GEOPOS)
        List<GeoCoordinate> posList = jedis.geopos(geoKey, "Beijing", "Shanghai", "NonExistCity");
        System.out.println("\n获取城市坐标:"+posList);
        for (int i = 0; i < posList.size(); i++) {
            GeoCoordinate coord = posList.get(i);
            System.out.println("coord222"+coord);
            if (coord != null) {
                System.out.println("城市: " + (i == 0 ? "Beijing" : i == 1 ? "Shanghai" : "NonExistCity")
                        + " - 经度: " + coord.getLongitude() + ", 纬度: " + coord.getLatitude());
            } else {
                System.out.println("城市不存在");
            }
        }

        // 3. 计算两个位置之间的距离 (GEODIST)
        Double distance = jedis.geodist(geoKey, "Beijing", "Shanghai");
        System.out.println("\n北京到上海的距离: " + distance + " 米");

        // 4. 查找指定半径内的位置 (GEORADIUS)
        //System.out.println("\n查找北京周围 1000 公里内的城市:");
        //List<GeoRadiusResponse> radiusResults = jedis.georadius(geoKey, 116.405285, 39.904989, 1000000, GeoRadiusParam.geoRadiusParam().withDist().withCoord().sortAscending());

        //for (GeoRadiusResponse result : radiusResults) {
        //    System.out.println("城市: " + result.getMemberByString()
        //            + ", 距离: " + result.getDistance() + " 米"
        //            + ", 坐标: (" + result.getCoordinate().getLongitude()
        //            + ", " + result.getCoordinate().getLatitude() + ")");
        //}
        //
        //// 5. 删除测试数据
        //jedis.del(geoKey);
        //System.out.println("\n已清理测试数据");
    }
}
